package client.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;

import messages.MessageCourse;
import messages.MessageTeacherSubject;

import sharedspace.Course;
import sharedspace.Subject;
import sharedspace.User;

/**
 * This JPanel is a panel for Teacher. Before making the test, the teacher need to choose the subject and course.
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_SelectSubjectTest extends JPanel {

	public JButton btnOk=null;
	public JButton btnBack = null;
	protected User user;
	protected Client_GUI MS;
	public JComboBox<String> comboBoxTeacherSubject;
	public JComboBox<String> comboBoxCourse;
	private ArrayList<sharedspace.Subject> s;
	private ArrayList<sharedspace.Course> c;
	public String subjectChoosen;
	public String courseChoosen;
	public String numberOfSubject;
	public Subject subject;

	public GUI_SelectSubjectTest(Client_GUI ms,User user) {
		super();
		this.initializeit();
		this.user = user;
		this.MS=ms;
		this.getTeacherSubject();
	}//end constructor
	
	public void initializeit(){
				
		JLabel title=new JLabel("Create Test");
		title.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 24));
		title.setBounds(new Rectangle(295, 56, 500, 59));
		title.setForeground(Color.BLUE);
		this.add(title);
		
		btnBack = new JButton();	
		btnBack.setText("Back");
		btnBack.setFont(new Font("Arial", Font.PLAIN, 18));
		btnBack.setBounds(new Rectangle(76, 612, 107, 30));
		btnBack.setVisible(true);
		
		btnOk = new JButton("OK");
		btnOk.setText("Ok");
		btnOk.setFont(new Font("Arial", Font.PLAIN, 18));
		btnOk.setBounds(new Rectangle(203, 612, 107, 30));
		btnOk.setVisible(true);
		
		JPanel southPanel = new JPanel();
		add(southPanel, BorderLayout.SOUTH);
		
		JPanel northPanel = new JPanel();
		add(northPanel, BorderLayout.NORTH);
		northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));

		
		
		btnOk.addActionListener(new ActionListener() {
			/**
			 * This action call the function selectCourse(),and getTeacherSubject()
			 * to select the, from the courses and subject lists
			 */
			public void actionPerformed(ActionEvent e) {
				
				if (comboBoxTeacherSubject.getSelectedIndex()==0)  //subject not selected 
					showMessage("Error, Please Select Subject of Test ", "Input Error", JOptionPane.ERROR_MESSAGE);
				else if (comboBoxCourse.getSelectedIndex()>0){  //course not selected
					//comboBoxCourse.setSelectedIndex(0);
						courseChoosen = (String)comboBoxCourse.getSelectedItem(); // get the subject that was selected
					for(int i=0; i<c.size();i++){ 					// find the subject in arrayList receive from message
						if(courseChoosen==c.get(i).getCourseName()){
							courseChoosen=c.get(i).getCourseID();
						}}//end for
				    startCreatTest(numberOfSubject,courseChoosen);  //Creating a test
				}//end if
				    else showMessage("Error, Please Select the course", "Input Error", JOptionPane.ERROR_MESSAGE);
			}// end actionPerformed		

		});
		
		GUI_ImagePanel background =new GUI_ImagePanel(new ImageIcon("images/background1.jpg").getImage());
		background.setLocation(0, 0);
		background.setSize(new Dimension(1000, 700));
		this.setSize(1000, 700);
		this.setLayout(null);
		this.add(btnBack);
		this.add(btnOk);
		this.add(background);
		
		comboBoxCourse= new JComboBox<String>();
		comboBoxCourse.setFont(new Font("Tahoma", Font.BOLD, 14));
		comboBoxCourse.setBounds(621, 253, 120, 24);
		background.add(comboBoxCourse);
		
		JLabel lblCourse = new JLabel("Choose Course:");
		lblCourse.setBounds(621, 152, 164, 42);
		background.add(lblCourse);
		lblCourse.setFont(new Font("Monospaced", Font.BOLD, 18));
		
		comboBoxTeacherSubject= new JComboBox<String>();
		comboBoxTeacherSubject.setFont(new Font("Tahoma", Font.PLAIN, 14));
		comboBoxTeacherSubject.setBounds(336, 253, 120, 24);
		background.add(comboBoxTeacherSubject);
		
			JLabel lblSubject = new JLabel("Choose Subject:");
			lblSubject.setBounds(307, 152, 180, 42);
			background.add(lblSubject);
			lblSubject.setFont(new Font("Monospaced", Font.BOLD, 18));
		
		comboBoxTeacherSubject.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBoxTeacherSubject.getSelectedIndex() > 0){   //if a subject was selected 
					
					 subjectChoosen = (String)comboBoxTeacherSubject.getSelectedItem(); // get the subject that was selected
						for(int i=0; i<s.size();i++){ 					// find the subject in arrayList recieve from message
							if(subjectChoosen==s.get(i).getSubjectName()){
							numberOfSubject=s.get(i).getSubjectID();
							subject=s.get(i);
							}}//end for
					getCourses();      //get course list of the subject
					comboBoxCourse.setEnabled(true);
				}//end if
				else
					comboBoxCourse.setEnabled(false);  // course not selected , don't display the teacher list
			}

		});	
	} //end initializeIt
	
		
	/**
	 * This function display teacher Subject list in combo box 
	 * @param teacherArray - is array list of the teacher subjects
	 */
	public void teacherSubjectComboBox(ArrayList<Subject> teacherSubject) {
		comboBoxTeacherSubject.removeAllItems();  //clear fields 
		if(teacherSubject.size() > 0){   
			comboBoxTeacherSubject.setEnabled(true);
			comboBoxTeacherSubject.addItem("Choose...");
			for (int i=0; i<teacherSubject.size(); i++){
				comboBoxTeacherSubject.addItem(teacherSubject.get(i).getSubjectName());  //display the teacher Subject in combo box
			}
	    }// end if
	    else{
	    	comboBoxTeacherSubject.addItem("Not Available");
	    	comboBoxTeacherSubject.setEnabled(false);
	    }// end else
	}// end function teacherIDComboBox
	
	/**
	 * get the teacher courses to set in comoBox
	 */
	public void getTeacherSubject(){
		MessageTeacherSubject mTs = new MessageTeacherSubject(user);
		try{
			MS.client.sendToServer(mTs);// get list of teacher subject
			
			 s = (ArrayList<Subject>)MS.client.GetMessage();
		
			 teacherSubjectComboBox(s); 
			
			
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} 
		
	}// getTeacherSubject
	
public void showMessage(String message, String title, int messageType) {
	JOptionPane.showMessageDialog(this, message,title,messageType);		
}

/**
 * this function goes to GUI_CreateTest
 */		
private void startCreatTest(String subjectChoosen,String courseChoosen) {
	try{	
	MS.ct =(new GUI_CreateTest(MS,user,subjectChoosen,courseChoosen,subject));
	MS.setContentPane(MS.ct);
	//button back if press will go back to TeacherGUI
	MS.ct.btnBack.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			System.out.println("actionPerformed");
			MS.setContentPane(MS.tchr);
			
		}});
	}
	catch (Exception e1){
		// TODO Auto-generated catch block
		e1.printStackTrace();
		}
}//end startCreatTest
/**
 * This function display teacher Subject list in combo box 
 * @param teacherArray - is array list of the teacher subjects
 */
public void teacherCoursesComboBox(ArrayList<Course> teacherCourse) {
	comboBoxTeacherSubject.removeAllItems();  //clear fields 
	if(teacherCourse.size() > 0){   
		comboBoxCourse.setEnabled(true);
		comboBoxCourse.addItem("Choose...");
		for (int i=0; i<teacherCourse.size(); i++){
			comboBoxCourse.addItem(teacherCourse.get(i).getCourseName());  //display the teacher Subject in combo box
		}//end for
    }// end if
    else{
    	comboBoxCourse.addItem("Not Available");
    	comboBoxCourse.setEnabled(false);
    }// end else
}// end function teacherIDComboBox

/*
 * gets the courses of subject after selection
 */
private void getCourses() {

	MessageCourse mTc = new MessageCourse(user,numberOfSubject);
	try{
		MS.client.sendToServer(mTc);// get list of teacher subject
		
		 c = (ArrayList<Course>)MS.client.GetMessage();
	
		 teacherCoursesComboBox(c); 
		
		
} catch (IOException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
} 
	
}// getCourses


	
}//end GUI_SelectSubject
